using System.IO;
using System.Web.Mvc;
using System.Xml;
using XmlRpcMvc;
using mitasoft.Component.Helpers;
using mitasoft.Component.Services.Interfaces;
using mitasoft.Component.Enums;
using System.Xml.Linq;

namespace mitasoft.Web.Controllers
{

    public class XmlRpcController : Controller
    {
        #region fields

        private const string ManifestUri = "http://schemas.microsoft.com/wlw/manifest/weblog";
        
        #endregion

        #region ctor
        public XmlRpcController()
        {
        }
        #endregion

        #region actions

        public ActionResult Rsd()
        {
            var ms = new MemoryStream();
            var xmlWriter = XmlWriter.Create(ms);

            xmlWriter.WriteStartDocument();
            {
                xmlWriter.WriteStartElement("rsd", "http://archipelago.phrasewise.com/rsd");
                xmlWriter.WriteAttributeString("xmlns", "xsd", null, "http://www.w3.org/2001/XMLSchema");
                xmlWriter.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");
                xmlWriter.WriteAttributeString("version", "1.0");
                {
                    xmlWriter.WriteStartElement("service", string.Empty);
                    {
                        xmlWriter.WriteStartElement("engineName");
                        xmlWriter.WriteString("mitasoft engine");
                        xmlWriter.WriteEndElement();

                        xmlWriter.WriteStartElement("homePageLink");
                        xmlWriter.WriteString("http://moldovanu.net");
                        xmlWriter.WriteEndElement();

                        xmlWriter.WriteStartElement("apis");
                        {
                            xmlWriter.WriteStartElement("api");
                            xmlWriter.WriteAttributeString("name", "MetaWeblog");
                            xmlWriter.WriteAttributeString("preferred", "true");
                            xmlWriter.WriteAttributeString("apiLink", Url.Action("Endpoint", "XmlRpc"));
                            xmlWriter.WriteAttributeString("blogID", "1");
                            xmlWriter.WriteEndElement();
                        }
                        xmlWriter.WriteEndElement();

                    }
                    xmlWriter.WriteEndElement();
                }
                xmlWriter.WriteEndElement();
            }
            xmlWriter.WriteEndDocument();

            xmlWriter.Flush();
            ms.Position = 0;

            return new FileStreamResult(ms, "text/xml");
        }

        public ActionResult LiveWriter()
        {

            var options = new XElement(
                XName.Get("options", ManifestUri),
                new XElement(XName.Get("supportsAutoUpdate", ManifestUri), "Yes"),
                new XElement(XName.Get("clientType", ManifestUri), "Metaweblog"),
                new XElement(XName.Get("supportsKeywords", ManifestUri), "No"),
                new XElement(XName.Get("supportsCategories", ManifestUri), "Yes"),
                new XElement(XName.Get("supportsFileUpload", ManifestUri), "Yes"),
                new XElement(XName.Get("supportsCustomDate", ManifestUri), "Yes"),
                new XElement(XName.Get("supportsNewCategories", ManifestUri), "Yes")
                );

            var doc = new XDocument(new XElement(
                                        XName.Get("manifest", ManifestUri),
                                        options));

            return Content(doc.ToString(), "text/xml");
        }

        public ActionResult Endpoint()
        {
            return new XmlRpcResult();
        } 
        
        #endregion
    }
}
